package com.lingyu.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.lingyu.biz.IPraiseBiz;
import com.lingyu.biz.ITopicBiz;
import com.lingyu.pojo.Prasie;
import com.lingyu.pojo.Topic;
import com.lingyu.pojo.User;

@Controller
@RequestMapping("topic")
public class TopicController {
	@Autowired
	private ITopicBiz topicBiz;
	@Autowired
	private IPraiseBiz praiseBiz;
	
	@RequestMapping("/getalltopics")
	public String getAllTopics(Map<String,Object> map,HttpServletRequest request){
		
		List<Topic> topicList = topicBiz.getAllTopics();
		
		List<Prasie> prasies = topicBiz.getAllZan(((User)request.getSession().getAttribute("login")));
		
		
		
		System.out.println("........////");
		for (Prasie prasie : prasies) {
			System.out.println(prasie+"...............");
		}
		
		map.put("topicList", topicList);
		map.put("prasies", prasies);
		
		return "/index.jsp";
	}
	
	@RequestMapping("/addTopic")
	@ResponseBody
	public String addTopic(Topic topic,HttpServletRequest request){
		System.out.println("...........");
		System.out.println(topic);
		topic.setUser((User)request.getSession().getAttribute("login"));
		topic.setTime(new Date());
		System.out.println("......../////////.");
		System.out.println(topic);
		topicBiz.addTopic(topic);
		
		return "getalltopics.do";
	}
	
	@RequestMapping(value = "/ajaxupload")
	@ResponseBody
	public String ajaxUpload(@RequestParam("file") MultipartFile[] files, HttpServletRequest request)throws IllegalStateException, IOException {
		String filename = "";
		
		if (files.length > 0) {
			// 上传文件路径
			String path = request.getSession().getServletContext().getRealPath("/upload/");
			for (MultipartFile file : files) {
				// 上传文件名
				String originalFilename = file.getOriginalFilename();// 获取文件名
				
				filename = String.valueOf(System.currentTimeMillis())+"_"+originalFilename;
				File filepath = new File(path, filename);
				// 判断路径是否存在，如果不存在就创建一个
				if (!filepath.getParentFile().exists()) {
					filepath.getParentFile().mkdirs();
				}
				// 将上传文件保存到一个目标文件当中
				file.transferTo(new File(path + File.separator + filename));
//				file.transferTo(new File("D:\\lingyu\\maven_qqzone\\src\\main\\webapp\\upload" + File.separator + filename));
			}
		}

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("imgsName", filename);
		return jsonObject.toJSONString();
	}
	
	@RequestMapping(value = "/ajaxpraise")
	@ResponseBody
	public String ajacPraise(String name,String topicId,String userId){
		System.out.println(topicId+"....."+userId+"........"+name);
		Prasie praise = new Prasie(topicId, Integer.parseInt(userId));
		if(name.equals("zan")){
			praiseBiz.insert(praise);
		}else{
			praiseBiz.delete(praise);
		}
		return "success";
	}
	

}
